<?php


namespace app\common\model;

use think\facade\Db;
use think\facade\Cache;
use think\Model;

class Job extends Model
{
    protected $table = "ea_job";

    public static function positionlists($params) : array {
        $where = [];
        $page = request()->post('page');

        if (isset($params['search']) && $params['search']) {
            $search = $params['search'];
            $where[] = ['j.title|c.company_name','like',"%$search%"];
        }

        if (isset($params['company_id'])) {
            $where[] = ['j.company_id','=',$params['company_id']];
        }
        if (isset($params['cater_id']) && $params['cater_id']) {
            $where[] = ['j.cater_id','=',$params['cater_id']];
        }
        if (isset($params['xinzi']) && $params['xinzi']) {
            if ($params['xinzi'] == '面议') $where[] = ['j.salary','=',0];
            if ($params['xinzi'] == '1k-2k') $where[] = ['j.salary','=',1];
            if ($params['xinzi'] == '3k-5k') $where[] = ['j.salary','=',2];
            if ($params['xinzi'] == '5k-8k') $where[] = ['j.salary','=',3];
            if ($params['xinzi'] == '8K-10k') $where[] = ['j.salary','=',4];
            if ($params['xinzi'] == '10k-12k') $where[] = ['j.salary','=',5];
            if ($params['xinzi'] == '12K-15k') $where[] = ['j.salary','=',6];
            if ($params['xinzi'] == '15K-18k') $where[] = ['j.salary','=',7];
            if ($params['xinzi'] == '18k-20k') $where[] = ['j.salary','=',8];
            if ($params['xinzi'] == '20K以上') $where[] = ['j.salary','=',9];
        }
        // 融资
        if (isset($params['rongzi']) && $params['rongzi']) {
            if ($params['rongzi'] == '未融资') $where[] = ['c.financing','=',0];
            if ($params['rongzi'] == '天使轮') $where[] = ['c.financing','=',1];
            if ($params['rongzi'] == 'A轮') $where[] = ['c.financing','=',2];
            if ($params['rongzi'] == 'B轮') $where[] = ['c.financing','=',3];
            if ($params['rongzi'] == 'C轮') $where[] = ['c.financing','=',4];
            if ($params['rongzi'] == 'D轮及以上') $where[] = ['c.financing','=',5];
            if ($params['rongzi'] == '已上市') $where[] = ['c.financing','=',6];
            if ($params['rongzi'] == '不需要融资') $where[] = ['c.financing','=',7];
        }
        // 规模
        if (isset($params['guimo']) && $params['guimo']) {
            if ($params['guimo'] == '0-20人') $where[] = ['c.scale','=',0];
            if ($params['guimo'] == '20-99人') $where[] = ['c.scale','=',1];
            if ($params['guimo'] == '100-499人') $where[] = ['c.scale','=',2];
            if ($params['guimo'] == '500-999人') $where[] = ['c.scale','=',3];
            if ($params['guimo'] == '1000-9999人') $where[] = ['c.scale','=',4];
            if ($params['guimo'] == '10000人以上') $where[] = ['c.scale','=',5];
        }
        // 学历
        if (isset($params['xueli']) && $params['xueli']) {
            if ($params['xueli'] == '初中及以下') $where[] = ['j.academic_requirements','=',1];
            if ($params['xueli'] == '中专/中技') $where[] = ['j.academic_requirements','=',2];
            if ($params['xueli'] == '高中') $where[] = ['j.academic_requirements','=',3];
            if ($params['xueli'] == '大专') $where[] = ['j.academic_requirements','=',4];
            if ($params['xueli'] == '本科') $where[] = ['j.academic_requirements','=',5];
            if ($params['xueli'] == '硕士') $where[] = ['j.academic_requirements','=',6];
            if ($params['xueli'] == '博士') $where[] = ['j.academic_requirements','=',7];
            if ($params['xueli'] == '博后') $where[] = ['j.academic_requirements','=',8];
        }
        // 经验
        if (isset($params['jinyan']) && $params['jinyan']) {
            if ($params['jinyan'] == '一年以内') $where[] = ['j.experience_requirement','=',1];
            if ($params['jinyan'] == '1-3年') $where[] = ['j.experience_requirement','=',2];
            if ($params['jinyan'] == '3-5年') $where[] = ['j.experience_requirement','=',3];
            if ($params['jinyan'] == '5-10年') $where[] = ['j.experience_requirement','=',4];
            if ($params['jinyan'] == '10年以上') $where[] = ['j.experience_requirement','=',5];
        }
        // 发布时间
        if (isset($params['fabu']) && $params['fabu']) {
            if ($params['fabu'] == '一天以内') $where[] = ['j.add_time','>',strtotime("-1 day")];
            if ($params['fabu'] == '三天以内') $where[] = ['j.add_time','>',strtotime("-3 day")];
            if ($params['fabu'] == '七天以内') $where[] = ['j.add_time','>',strtotime("-7 day")];
            if ($params['fabu'] == '十五天以内') $where[] = ['j.add_time','>',strtotime("-15 day")];
            if ($params['fabu'] == '一个月以内') $where[] = ['j.add_time','>',strtotime("-30 day")];
        }

        if ($page == 1) {
            // 列表随机 先取出所有的数据id  在进行随机排序  存入缓存  防止下一页出现重复的数据
            $jobid = self::alias('j')
                ->join('company_management m', 'm.member_id = j.publisher','LEFT')
                ->join('company c', 'c.id = j.company_id','LEFT')
                ->order('j.id desc')
                ->where('j.status',2)
                ->where($where)
                ->column('j.id');
            // 随机打乱
            shuffle($jobid);
            Cache::set('positionlists'.app('getsystem')->MacAddInfo(),$jobid);
        } else {
            $jobid = Cache::get('positionlists'.app('getsystem')->MacAddInfo());
        }

        $ids = array_slice($jobid,($page * 10) - 10,10);


        $data = self::alias('j')
            ->join('company_management m', 'm.member_id = j.publisher','LEFT')
            ->join('company c', 'c.id = j.company_id','LEFT')
            ->where('j.id','in',$ids)
            ->field('j.id,j.title,j.academic_requirements,j.publisher,j.salary,j.experience_requirement,j.company_id,c.company_name as cName
            ,c.scale,m.position,c.logo,j.cater_id,j.status,j.add_time,c.industry,c.financing,c.welfare,j.address')
            ->select();

        foreach ($data as $key => $item) {

            if ($item['welfare']) {
                $welfare = json_decode($item['welfare']);

                $data[$key]['welfare'] = getwelfare($welfare);
            } else {
                $data[$key]['welfare'] = [];
            }

            $management = Db::table('ea_company_management c')
                ->join('ea_member m','c.member_id = m.id')
                ->where('c.company_id',$item['company_id'])
                ->field('m.name,position,m.avatar,sex')
                ->find();

            if ($management) {
                $data[$key]['personnel_name'] = $management['name'] ? $management['name'] : '招聘员';
                $data[$key]['position'] = $management['position'] ? $management['position'] : '人事部';
                $data[$key]['avatar'] = $management['avatar'];
            }

            $data[$key]['industry'] = industrytype($item['industry']); // 行业
            $data[$key]['financing'] = financingtype($item['financing']); // 行业
            $data[$key]['scale'] = scaletype($item['scale']); // 公司规模
            $data[$key]['academic_requirements'] = academic_requirements($item['academic_requirements']);
            $data[$key]['experience_requirement'] = experience_requirement($item['experience_requirement']);

        }

        return ['list'=>['total'=>count($jobid),'per_page'=>10,'current_page'=>$page,'last_page'=>ceil(count($jobid)/10),'data'=>$data]];
        // return ['list'=>$data];
    }

    /**
     * 获取职位列表
     * @param  [type] $cater_id [description]
     * @param  [type] $keyword  [description]
     * @return [type]           [description]
     */
    public static function lists($cater_id,$keyword)
    {
        $page = request()->get('page');

        if ($page==1) {
            $where = [];

            if ($cater_id) {
                $where[] = ['cater_id','=',$cater_id];
            }
            if ($keyword) {
                $where[] = ['j.title|company_name','like',"%$keyword%"];
            }

            $jobid = self::alias('j')
                ->join('company_management m', 'm.member_id = j.publisher','LEFT')
                ->join('company c', 'c.id = j.company_id','LEFT')
                ->order('j.id desc')
                ->where($where)
                ->where('j.status',2)
                ->column('j.id');

            shuffle($jobid);
            Cache::set('lists'.request()->users['id'],$jobid);
        } else {
            $jobid = Cache::get('lists'.request()->users['id']);
        }


        $ids = array_slice($jobid,($page * 10) - 10,10);
        if (!$ids) {
            return ['list'=>['total'=>0,'per_page'=>10,'current_page'=>10,'last_page'=>1,'data'=>[]]]; 
        }
        $orderRaw = "field(j.id,".implode($ids, ",").")";

        $field = ['j.id', 'j.title', 'j.area_no', 'j.academic_requirements', 'j.publisher'
            , 'j.salary', 'j.experience_requirement', 'j.company_id' , 'c.company_name as cName'
            , 'c.scale', 'm.position','c.logo','j.cater_id','j.status','short_name'];

        $data = self::alias('j')
            ->join('company_management m', 'm.member_id = j.publisher','LEFT')
            ->join('company c', 'c.id = j.company_id','LEFT')
            ->where('j.id','in',$ids)
            ->orderRaw($orderRaw)
            ->field($field)
            ->where('j.status',2)
            ->select();

        foreach ($data as $key => $item) {
            $data[$key]['salary'] = salary($item['salary']);

            $data[$key]['scale'] = scaletype($item['scale']); // 公司规模
            $data[$key]['area_no'] = areaNoText($item['area_no']); // 上班地区

            if (!$item['logo']) {
                $item['logo'] = "https://www.zhuhaoduo.cc/static/home/images/companys.png";
            }

            $data[$key]['academic_requirements'] = academic_requirements($item['academic_requirements']);
            $data[$key]['experience_requirement'] = experience_requirement($item['experience_requirement']);

        }

        return ['list'=>['total'=>count($jobid),'per_page'=>10,'current_page'=>$page,'last_page'=>ceil(count($jobid)/10),'data'=>$data]];
    }

    /**
     * 职位列表
     * @param  [type] $companyid [description]
     * @param  [type] $status    [description]
     * @return [type]            [description]
     */
    public function joblist($companyid,$status)
    {
        $field = ['id','title','academic_requirements','experience_requirement','salary','status','cater_id','peoplenum'];

        $where = [];
        if($status != 5) {
            $where['status'] = $status;
        }
        $data = $this->where('company_id', '=', $companyid)->where($where)->field($field)->order('id desc')->select()->toArray();


        $management = Db::table('ea_company_management c')
            ->join('ea_member m','c.member_id = m.id')
            ->join('ea_company y','c.company_id = y.id')
            ->where('c.company_id',$companyid)
            ->field('m.name,position,avatar,company_name,scale,m.sex')
            ->find();

        foreach ($data as $key => $item) {
            $data[$key]['salary'] = salary($item['salary']);
            $data[$key]['academic_requirements'] = academic_requirements($item['academic_requirements']);
            $data[$key]['experience_requirement'] = experience_requirement($item['experience_requirement']);

            if ($item['status'] == 1) {
                $data[$key]['status'] = "未审核";
            }
            if ($item['status'] == 2) {
                $data[$key]['status'] = "开放中";
            }
            if ($item['status'] == 3) {
                $data[$key]['status'] = "审核失败";
            }
            if ($item['status'] == 4) {
                $data[$key]['status'] = "已关闭";
            }

            $data[$key]['cater_name'] = '';
            if ($item['cater_id']) {
                $data[$key]['cater_name'] = Db::table('ea_caterory')->where('id',$item['cater_id'])->value('name');
            }

            if ($management) {
                $data[$key]['position'] = $management['position'] ? $management['position'] : '人事部';
                $data[$key]['name'] = $management['name'] ? $management['name'] : '招聘员';
                $data[$key]['avatar'] = $management['avatar'];
                $data[$key]['company_name'] = $management['company_name'];
                $data[$key]['scale'] = scaletype($management['scale']);
            }
        }

        return ['list'=>$data];
    }

    /**
     * 获取热门职位
     * @return mixed
     */
    public function position()
    {
        $joblist = $this->alias('j')
            ->join('ea_caterory c','j.cater_id = c.id','left')
            ->where(['j.status'=>2,'c.homestart'=>1])
            ->group('cater_id')
            ->limit(9)
            ->field('cater_id as id,c.name')
            ->select()
            ->toarray();

        return $joblist;
    }

    /**
     * 推荐职位
     * @return [type] [description]
     */
    public static function recommend()
    {
        $field = ['j.id','title','academic_requirements','experience_requirement','salary','company_id','scale','short_name','financing'];
        
        $data = self::alias('j')
            ->join('ea_company c','j.company_id = c.id')
            ->where('j.status',2)
            ->field($field)
            ->limit(10)
            ->orderRaw('rand()')
            ->select();

        foreach ($data as $key => $item) {
            $data[$key]['salary'] = salary($item['salary']);
            $data[$key]['academic_requirements'] = academic_requirements($item['academic_requirements']);
            $data[$key]['experience_requirement'] = experience_requirement($item['experience_requirement']);
            $data[$key]['financing'] = financingtype($item['financing']);
            $data[$key]['scale'] = scaletype($item['scale']);


            $management = Db::table('ea_company_management c')
                ->join('ea_member m','c.member_id = m.id')
                ->where('c.company_id',$item['company_id'])
                ->field('m.name,position,m.avatar,m.sex')
                ->find();

            if ($management) {
                $data[$key]['personnel_name'] = $management['name'] ? $management['name'] : '招聘员';
                $data[$key]['position'] = $management['position'] ? $management['position'] : '人事部';
                $data[$key]['avatar'] = $management['avatar'];
            }
        }

        return $data;
    }

    /**
     * 职位列表检索
     * @param  [type] $companyid [description]
     * @param  [type] $status    [description]
     * @return [type]            [description]
     */
    public function userlist()
    {
        $page = request()->get('page');

        if ($page==1) {
            $jobid = $this->getjobid();
            Cache::set('joblist'.request()->users['id'],$jobid);
        } else {
            $jobid = Cache::get('joblist'.request()->users['id']);
        }

        $ids = array_slice($jobid,($page * 10) - 10,10);

        $data = $this->alias('j')
            ->join('ea_company c','j.company_id = c.id')
            ->field('j.id,title,c.area_no,academic_requirements,experience_requirement,salary,company_id,company_name,short_name,scale,financing')
            ->where('j.id','in',$ids)
            ->select();

        foreach ($data as $key => $item) {
            $data[$key]['academic_requirements'] = academic_requirements($item['academic_requirements']);
            $data[$key]['experience_requirement'] = experience_requirement($item['experience_requirement']);

            $data[$key]['area_no'] = areaNoText($item['area_no']);
            $data[$key]['scale'] = scaletype($item['scale']);
            $data[$key]['financing'] = financingtype($item['financing']);
            $data[$key]['salary'] = salary($item['salary']);


            $management = Db::table('ea_company_management c')
                ->join('ea_member m','c.member_id = m.id')
                ->where('c.company_id',$item['company_id'])
                ->field('m.name,position,m.avatar')
                ->find();

            if ($management) {
                $data[$key]['personnel_name'] = $management['name'] ? $management['name'] : '招聘员';
                $data[$key]['position'] = $management['position'] ? $management['position'] : '人事部';
                $data[$key]['avatar'] = $management['avatar'];
            }
        }

        return ['list'=>['total'=>count($jobid),'per_page'=>10,'current_page'=>$page,'last_page'=>ceil(count($jobid)/10),'data'=>$data]];
    }

    /**
     * 获取职位列表id
     * @return [type] [description]
     */
    public function getjobid()
    {
        $jobid = $this->alias('j')
            ->join('ea_company c','j.company_id = c.id')
            ->where('j.status',2)
            ->column('j.id');

        shuffle($jobid);

        return $jobid;
    }

    /**
     * 用户投递简历列表
     * @param  [type] $member_id [description]
     * @return [type]            [description]
     */
    public function deliverylist($member_id)
    {
        $field = ['j.id','title','academic_requirements','experience_requirement','salary','j.company_id','company_name','scale','j.status','j.peoplenum','j.address'];

        $where = [];

        $result = Db::table('ea_mesg')->alias('m')
            ->join('ea_job j','j.id = m.job_id')
            ->join('ea_company c','j.company_id = c.id')
            ->where(['sendtype'=>1,'type'=>1,'member_id'=>$member_id])
            ->where($where)
            ->field($field)
            ->order('id desc')
            ->paginate(10);

        $data = $result->toArray();

        foreach ($data['data'] as $key => $value) {
            $data['data'][$key]['scale'] = scaletype($value['scale']);
            $data['data'][$key]['academic_requirements'] = academic_requirements($value['academic_requirements']);
            $data['data'][$key]['experience_requirement'] = experience_requirement($value['experience_requirement']);
            $data['data'][$key]['salary'] = salary($value['salary']);

            $management = Db::table('ea_company_management c')
                ->join('ea_member m','c.member_id = m.id')
                ->where('c.company_id',$value['company_id'])
                ->field('m.name,position,m.avatar,m.sex')
                ->find();

            if ($management) {
                $data['data'][$key]['personnel_name'] = $management['name'] ? $management['name'] : '招聘员';
                $data['data'][$key]['position'] = $management['position'] ? $management['position'] : '人事部';
                $data['data'][$key]['avatar'] = $management['avatar'];
            }
        }

        return ['list'=>$data];
    }

    /**
     * 用户收藏职位列表
     * @param  [type] $member_id [description]
     * @return [type]            [description]
     */
    public function collectionlist($member_id)
    {
        $field = ['j.id','title','academic_requirements','experience_requirement','salary','j.company_id','company_name','scale','j.status','j.peoplenum','c.area_no'];

        $result = Db::table('ea_favorites')->alias('m')
            ->join('ea_job j','j.id = m.job_id')
            ->join('ea_company c','j.company_id = c.id')
            ->where(['m.type'=>1,'member_id'=>$member_id])
            ->field($field)
            ->order('id desc')
            ->paginate(10);

        $data = $result->toArray();

        foreach ($data['data'] as $key => $value) {
            $data['data'][$key]['area_no'] = areaNoText($value['area_no']); // 上班地区
            $data['data'][$key]['scale'] = scaletype($value['scale']);
            $data['data'][$key]['academic_requirements'] = academic_requirements($value['academic_requirements']);
            $data['data'][$key]['experience_requirement'] = experience_requirement($value['experience_requirement']);

            $data['data'][$key]['salary'] = salary($value['salary']);

            $management = Db::table('ea_company_management c')
                ->join('ea_member m','c.member_id = m.id')
                ->where('c.company_id',$value['company_id'])
                ->field('m.name,position,m.avatar,sex')
                ->find();

            if ($management) {
                $data['data'][$key]['personnel_name'] = $management['name'] ? $management['name'] : '招聘员';
                $data['data'][$key]['position'] = $management['position'] ? $management['position'] : '人事部';
                $data['data'][$key]['avatar'] = $management['avatar'];
            }
        }

        return ['list'=>$data];
    }

    /**
     * 获取职位
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getjob($id)
    {
        $field = ['id','title','academic_requirements','experience_requirement','salary','status','detail','company_id','address','cater_id','detail','peoplenum'];

        $data = $this->where('id',$id)->field($field)->find();

        $data['academic_id'] = $data['academic_requirements'];  // 学历要求
        $data['experience_id'] = $data['experience_requirement'];  //  经验要求

        $data['academic_requirements'] = academic_requirements($data['academic_requirements']);  // 学历要求
        $data['experience_requirement'] = experience_requirement($data['experience_requirement']);  //  经验要求

        // 沟通
        $data['communicate'] = Db::table('ea_mesg')->where('sendtype',1)->where('type',1)->where('job_id',$id)->count();
        // 查看
        $data['see'] = Db::table('ea_browse')->where('type',1)->where('job_id',$id)->count();;

        $ea_company = Db::table('ea_company')->where('id',$data['company_id'])->field('company_name,industry,financing,logo,scale')->find();


        $ea_company['scale'] = scaletype($ea_company['scale']);
        $ea_company['industry'] = industrytype($ea_company['industry']);
        $ea_company['financing'] = financingtype($ea_company['financing']);

        $data['company'] = $ea_company;

        $data['salary'] = (string) $data['salary'];
        $data['salary_text'] = salary($data['salary']);
        

        $data['cater_name'] = '';
        if ($data['cater_id']) {
            $data['cater_name'] = Db::table('ea_caterory')->where('id',$data['cater_id'])->value('name');
        }
        return $data;
    }

    /**
     * 相似职位
     * @return [type] [description]
     */
    public function similar($cater_id)
    {
        $field = ['j.id', 'j.title', 'j.academic_requirements', 'j.publisher'
            , 'j.salary', 'j.experience_requirement', 'j.company_id' , 'c.company_name as cName'
            , 'c.scale', 'm.position','c.short_name','m.member_id','j.peoplenum','j.address','c.area_no','c.logo'];

        $data = $this->alias('j')
            ->join('company_management m', 'm.member_id = j.publisher','LEFT')
            ->join('company c', 'c.id = j.company_id','LEFT')
            ->where('j.status',2)
            ->orderRaw('rand()')
            ->field('j.id,j.title,j.academic_requirements,j.publisher
            ,j.salary,j.experience_requirement,j.company_id,c.company_name as cName,c.scale,m.position,c.short_name,m.member_id,j.peoplenum,j.address,c.area_no,c.logo')
            ->orderRaw('rand()')
            ->limit(10)
            ->select()
            ->toArray();

        foreach ($data as $key => $value) {
            $data[$key]['scale'] = scaletype($value['scale']);
            $data[$key]['salary'] = salary($value['salary']);
            $data[$key]['academic_requirements'] = academic_requirements($value['academic_requirements']);
            $data[$key]['experience_requirement'] = experience_requirement($value['experience_requirement']);
            $data[$key]['area_no'] = areaNoText($value['area_no']); // 地区

            $ea_member = Db::table('ea_member')->where('id',$value['member_id'])->field('name,avatar,sex')->find();

            $data[$key]['personnel_name'] = $ea_member['name'] ? $ea_member['name'] : '招聘员';
            $data[$key]['position'] = $value['position'] ? $value['position'] : '人事部';
            $data[$key]['avatar'] = $ea_member['avatar'];

            if (!$value['logo']) {
                $data[$key]['logo'] = "/static/home/images/companys.png";
            }
        }

        return $data;
    }

    /**
     * 公司职位列表
     * @return [type] [description]
     */
    public static function companylists($params,$company_id): array
    {
        $where = [];

        if ($params['search']) {
            $search = $params['search'];
            $where[] = ['j.title','like',"%$search%"];
        }
        if ($params['status'] == "待审核") {
            $where[] = ['j.status','=',1];
        }
        if ($params['status'] == "开启中") {
            $where[] = ['j.status','=',2];
        }
        if ($params['status'] == "审核失败") {
            $where[] = ['j.status','=',3];
        }
        if ($params['status'] == "关闭") {
            $where[] = ['j.status','=',4];
        }

        $field = ['j.id', 'j.title', 'j.academic_requirements', 'j.publisher'
            , 'j.salary', 'j.experience_requirement', 'j.company_id' , 'c.company_name as cName'
            , 'c.scale', 'm.position','c.logo','j.cater_id','j.status','j.add_time','j.address','j.peoplenum'];

        $data = self::alias('j')
            ->join('company_management m', 'm.member_id = j.publisher','LEFT')
            ->join('company c', 'c.id = j.company_id','LEFT')
            ->order('j.id desc')
            ->where($where)
            ->where('j.company_id',$company_id)
            ->field($field)
            ->paginate(10);

        foreach ($data as $key => $item) {
            $data[$key]['academic_requirements'] = academic_requirements($item['academic_requirements']);
            $data[$key]['experience_requirement'] = experience_requirement($item['experience_requirement']);
            $data[$key]['salary'] = salary($item['salary']);
            // 浏览
            $data[$key]['browse'] = Db::table('ea_browse')->where('type',1)->where('job_id',$item['id'])->count();
            // 职位投递
            $data[$key]['delivery'] = Db::table('ea_mesg')->where('sendtype',1)->where('type',1)->where('job_id',$item['id'])->count();

        }

        return ['list'=>$data];
    }

    /**
     * 一对一关联
     * 职位详情关联公司详情
     */
    public function company()
    {
        return $this->hasOne(Company::class,'id','company_id');
    }
}
